{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f4c7adfd",
   "metadata": {},
   "source": [
    "## Numpy随机模块\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4ad498ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.11990301, 0.19950057],\n",
       "       [0.14026699, 0.17031481],\n",
       "       [0.92593751, 0.5277405 ]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 所有的值都是从0到1\n",
    "np.random.rand(3,2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f70d2f76",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[4, 7, 8, 6],\n",
       "       [3, 3, 1, 3],\n",
       "       [7, 4, 2, 7],\n",
       "       [1, 2, 5, 3],\n",
       "       [8, 2, 6, 0]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 返回的是随机整数，左闭右开\n",
    "np.random.randint(10,size=(5,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "03d268e6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.06752320480636853"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.rand()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f9e37192",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5873126587129599"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.random_sample()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "86d640ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 0, 8])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.randint(0,10,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "4d5ea22b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.08091281, -0.01645862,  0.17931327,  0.11426764,  0.18644395,\n",
       "        0.09853165, -0.08152984,  0.20039546,  0.16368305,  0.00690737])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mu,sigma = 0,0.1\n",
    "np.random.normal(mu, sigma, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ecc1b5fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.set_printoptions(precision=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "a76ea0b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.2 , -0.1 ,  0.14, -0.2 , -0.02, -0.19, -0.14,  0.12, -0.11,\n",
       "       -0.  ])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mu,sigma = 0,0.1\n",
    "np.random.normal(mu, sigma, 10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8b44be4",
   "metadata": {},
   "source": [
    "## 洗牌"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "5f30d337",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tang_array = np.arange(10)\n",
    "tang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "018cbfc5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 7, 9, 3, 1, 2, 6, 8, 5, 0])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.shuffle(tang_array)\n",
    "tang_array"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "171e4b70",
   "metadata": {},
   "source": [
    "## 随机的种子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "593b7ef0",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "d86c0be7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-0.17,  0.03,  0.12, -0.03,  0.1 ,  0.05,  0.02, -0.11, -0.02,\n",
       "        0.03])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mu,sigma = 0,0.1\n",
    "np.random.normal(mu, sigma, 10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0529994",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
